# add some documentation to this code you slacker
#   scott lee davis   skawtus@gmail.com
#

require 'gdata'

class ContactshuffleController < ApplicationController


 # before_filter :setup_client
  
  CONTACTS_FEED = CONTACTS_SCOPE + 'contacts/default/full/'
		APPS_DOMAIN = 'contactshuffle.com'
 
 		CONTACTS_MAX = '11'
		#CONTACTS_FEED2 = 'http://www.google.com/m8/feeds/contacts/default/full?max-results=' + CONTACTS_MAX
		CONTACTS_FEED2 = 'https://www.google.com/m8/feeds/contacts/contactshuffle.com/full?max-results=' + CONTACTS_MAX
		@FINAL_CONTACTS_FEED  = ''
 
  def all

    @client = GData::Client::Contacts.new
    @user = []
    @contacts = []
    @action = []

		
    if params[:token].nil? and session[:token].nil?

      next_url = url_for :controller => self.controller_name, :action => self.action_name
      secure = false
      @authsub_link = @client.authsub_url(next_url, secure, true)

 
    elsif params[:token] and session[:token].nil?
    
      @client.authsub_token = params[:token]
      session[:token] = @client.auth_handler.upgrade()	
			@client.authsub_token = session[:token] 
			
		end

		if !params[:logout].nil? and !session[:token].nil?
		
			@client.authsub_token = session[:token] 
			@client.auth_handler.revoke
			session[:token] = nil
			
		end
	
		if !session[:token].nil?
		
		
			@client.authsub_token = session[:token] 
					
			if !params[:contactid].nil? #contact added hack... fix me...
			
				@action.push('added contact '+params[:contactid]+' '+params[:contactemail])
 # creating, updating, and deleting a playlist


	#for details for protocol, see
	# http://code.google.com/googleapps/domain/shared_contacts/gdata_shared_contacts_api_reference.html
	# defines the full contact xml...
  new_entry = <<-EOF
<atom:entry xmlns:atom='http://www.w3.org/2005/Atom'
    xmlns:gd='http://schemas.google.com/g/2005'>
  <atom:category scheme='http://schemas.google.com/g/2005#kind'
    term='http://schemas.google.com/contact/2008#contact' />
  <gd:name>
     <gd:fullName>#{params[:contactid]}</gd:fullName>
  </gd:name>
  <atom:content type='text'>Notes</atom:content>
  <gd:email rel='http://schemas.google.com/g/2005#work'
    primary='true'
    address='#{params[:contactemail]}' displayName='#{params[:contactid]}' />
</atom:entry>
  EOF

				response = @client.post('https://www.google.com/m8/feeds/contacts/contactshuffle.com/full', new_entry).to_xml
        #@action.push(response)      #ugly lame not handling the repsonse well
                                 
			end
			

			#@contacts.push(@FINAL_CONTACTS_FEED)
			
			feed = @client.get(CONTACTS_FEED2).to_xml 
			#@contacts.push(feed)

			@user = feed.elements['id'].text
			@contacts.push('<div id="contacts">')

			feed.elements.each('entry') do |entry|
			
				@contacts.push('<div id="',entry.elements['title'].text,'">',entry.elements['title'].text,' ')

				entry.elements.each('gd:email') do |email|
					if email.attribute('primary')
						@contacts.push('<div id="',email.attribute('address').value,'">',email.attribute('address').value,'</div>')
					end
        end

				@contacts.push('</div>')
				
			end
		
			@contacts.push('</div>')
		
		end
			 
		

		render :controller => 'contactshuffle' , :action => 'all'

    
  end


end
